#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "../include/narytree.h"
#include "../include/string_tok.h"


int delete_recursively(t_node* node);

int validate_path(t_node **root,char *path)
{
	if(*root == NULL)
	{
		return -1;
	}
	else
	{
		char **token;
		t_node *cur_node;
		int nop;
		nop=get_tokens(&token,path,'/');
		if(strcmp(path,"/")==0) 
		{
			return 0;
		}
		cur_node=*root;


	}

}


int insert_node(t_node** root, fd* data)
{
    t_node *new_node;
    char **token;
    t_node *cur_node;
    int nop,i,j;
    if(*root==NULL)
    {
        if(strcmp(data->file_name,"/")==0 && strcmp(data->path,"/")==0)
        {
	   
            new_node=(t_node *)malloc(sizeof(t_node));
            new_node->is_deleted=FALSE;
            new_node->left_child=NULL;
            new_node->right_siblings=NULL;
            new_node->parent=NULL;
            new_node->data=data;
            *root=new_node;
            return 0;
        }
        return -1;
    }
    else
    {
		nop=get_tokens(&token,data->path,'/');
		if(strcmp(data->path,"/")==0) nop--;
		cur_node=*root;
		for(i=1;i<nop;i++)
		{
			cur_node=cur_node->left_child;
			while(cur_node!=NULL && (strcmp(cur_node->data->file_name,token[i])!=0 ||  cur_node->data->is_dir==0 || cur_node->is_deleted==TRUE))
			{
				cur_node=cur_node->right_siblings;
			}
			if(cur_node==NULL)
			{
				return -1;
			}
		}
		
		if(cur_node->left_child==NULL)
		{
			new_node=(t_node *)malloc(sizeof(t_node));
            	new_node->is_deleted=FALSE;
            	new_node->left_child=NULL;
            	new_node->right_siblings=NULL;	   
            	new_node->parent=cur_node;
            	new_node->data=data;
            	cur_node->left_child=new_node;
            	return 0;
		}
		cur_node=cur_node->left_child;
		while(cur_node->right_siblings!=NULL)
		{
            if(strcmp(cur_node->data->path,data->path) == 0 && strcmp(cur_node->data->file_name,data->file_name) == 0 && cur_node->is_deleted==FALSE)
                return -1;
            cur_node=cur_node->right_siblings;
		}
		if(strcmp(cur_node->data->path,data->path) == 0 && strcmp(cur_node->data->file_name,data->file_name) == 0 && cur_node->is_deleted==FALSE)
                return -1;
		new_node=(t_node *)malloc(sizeof(t_node));
            new_node->is_deleted=FALSE;
            new_node->left_child=NULL;
	      new_node->right_siblings=NULL;
      	new_node->parent=cur_node->parent;
            new_node->data=data;
            cur_node->right_siblings=new_node;
	      return 0;
	}
}


int delete_node(t_node** root,char* fullpath)
{
    char **token;
    t_node *cur_node,*parent_node;
    int nop,i,j,count;
    if(*root==NULL)
    {
         return 1;
    }
    else
    {
		nop=get_tokens(&token,fullpath,'/');
		if(strcmp(fullpath,"/")==0) nop--;
		cur_node=*root;
		for(i=1;i<nop;i++)
		{
			cur_node=cur_node->left_child;
			while(cur_node!=NULL && (strcmp(cur_node->data->file_name,token[i])!=0 ||   cur_node->is_deleted==TRUE))
			{
				cur_node=cur_node->right_siblings;
			}
			if(cur_node==NULL)
				return 1;
		}
		delete_recursively(cur_node->left_child);
		cur_node->is_deleted=TRUE;
		return 0;
	}

}

t_node* returnnode(t_node* root, char* fullpath)
{
    char **token;
    t_node *cur_node,*parent_node;
    int nop,i,j,count;
    if(root==NULL)
    {
         return NULL;
    }
    else
    {
		nop=get_tokens(&token,fullpath,'/');printf("nop:%d",nop);
		if(strcmp(fullpath,"/")==0) nop--;
		cur_node=root;
		for(i=1;i<nop;i++)
		{
			cur_node=cur_node->left_child;
			while(cur_node!=NULL && (strcmp(cur_node->data->file_name,token[i])!=0 ||  cur_node->data->is_dir==0 || cur_node->is_deleted==TRUE))
			{
				cur_node=cur_node->right_siblings;
			}
			if(cur_node==NULL)
				return NULL;
		}
		return cur_node;
	}
}
fd* get_node(t_node** root,char* fullpath)
{
    char **token;
    t_node *cur_node,*parent_node;
    int nop,i,j,count;
    if(*root==NULL)
    {
         return NULL;
    }
    else
    {
		nop=get_tokens(&token,fullpath,'/');
		if(strcmp(fullpath,"/")==0) nop--;
		cur_node=*root;
		for(i=1;i<nop;i++)
		{
			cur_node=cur_node->left_child;
			while(cur_node!=NULL && (strcmp(cur_node->data->file_name,token[i])!=0 ||  cur_node->data->is_dir==0 || cur_node->is_deleted==TRUE))
			{
				cur_node=cur_node->right_siblings;
			}
			if(cur_node==NULL)
				return NULL;
		}
		return cur_node->data;
	}
}

int get_children(t_node** root,char* fullpath,fd **data[])
{
    char **token;
    t_node *cur_node,*parent_node;
    int nop,i,j,count;
    if(*root==NULL)
    {
         return -1;
    }
    else
    {
		nop=get_tokens(&token,fullpath,'/');
		if(strcmp(fullpath,"/")==0) nop--;
		cur_node=*root;
		for(i=1;i<nop;i++)
		{
			cur_node=cur_node->left_child;
			while(cur_node!=NULL && (strcmp(cur_node->data->file_name,token[i])!=0 ||  cur_node->data->is_dir==0 || cur_node->is_deleted==TRUE))
			{
				cur_node=cur_node->right_siblings;
			}
			if(cur_node==NULL)
				return -1;
		}
		count=0;
		parent_node=cur_node;
		cur_node=cur_node->left_child;
		while(cur_node!=NULL)
		{
			if(cur_node->is_deleted==FALSE) count++;
			cur_node=cur_node->right_siblings;
		}
		//if(count==0)return -1;
		*data=(fd**)malloc(count*sizeof(fd*));
		cur_node=parent_node->left_child;
		for(i=0;i<count;)
		{
			if(cur_node->is_deleted==FALSE)
            {
               (*data)[i]=cur_node->data;
               i++;
            }
			cur_node=cur_node->right_siblings;
		}
		return count;
	}
}


int move_node(t_node* root,char* source_path,char* destination_path)
{
    return 0;
}

int print_tree(t_node* root)
{
    int i;
	if(root!=NULL)
    {
        if(root->is_deleted==FALSE)
    	{
    		printf("%s/%s dir? %d \t size : %d\t",root->data->path,root->data->file_name,root->data->is_dir,root->data->size);
    		for(i=0;root->data->blocks[i]!=-1;i++)
              printf("%d\t",root->data->blocks[i]);
            printf("\n");
        }
		print_tree(root->left_child);
		print_tree(root->right_siblings);
     }
	return 0;
}

int delete_recursively(t_node* node)
{
	if(node!=NULL)
	{
		delete_recursively(node->left_child);
		delete_recursively(node->right_siblings);
		node->is_deleted=TRUE;
	}
	return 0;
}



